/*!
 * imageCrawler.js v0.1
 *
 * @Author Alex Peta, 2012
 * http://www.alexpeta.ro/
 * Licensed under GPL : http://www.gnu.org/copyleft/gpl.html
 *
        _                      _                     
   __ _| | _____  ___ __   ___| |_  __ _   _ __ ___  
  / _` | |/ _ \ \/ / '_ \ / _ \ __|/ _` | | '__/ _ \ 
 | (_| | |  __/>  <| |_) |  __/ |_| (_| |_| | | (_) |
  \__,_|_|\___/_/\_\ .__/ \___|\__|\__,_(_)_|  \___/ 
                   |_|                               

 *
 */
var imageCrawler = {
	containerName : 'imageCrawlerContainer',
	GenerateContainer :  function() {
		return '<div class="imageCrawler-container" id="'+imageCrawler.containerName+'"><table><tr> <td> <span>Hey Joe, here are the images :</span></td><td style="width:160px;padding-top:5px;"><a href="javascript:imageCrawler.SaveBookmark();" title="Add to ImageCrawler">Add To ImageCrawler</a></td></tr><tr>'+
		'<td><table>'+imageCrawler.InsertImages()+'</table></td>'+
		'</tr></table></div>';
	},
	DeleteContainer : function(){
		var element = document.getElementById(imageCrawler.containerName);
		var body = document.getElementsByTagName('body')[0];
		body.removeChild(element);
	},
	HasJquery : function(){
	   	try
		{
			var jqueryIsLoaded=jQuery;
			jQueryIsLoaded=true;
		}
		catch(err)
		{
			var jQueryIsLoaded=false;
		}

		if(jQueryIsLoaded)
		{
			return true;
		}	
		else
		{
			return false;
		}
	},
	SaveBookmark : function() {
		alert('Saving images ....etc..! Done!');
		var t = setTimeout("imageCrawler.DeleteContainer()","2000");
	},
	InsertImages : function(){
    	var imagesArr = document.getElementsByTagName('img');

    	if(imagesArr == null || imagesArr.length==0)
    	{
    		return '<tr><td><span style="">No Images found :(</span></td></tr>';
    	}
        else
        {
	    	for(var i=0;i<imagesArr.length;i++)
	    	{
	    		if(imagesArr[i].src != null && imagesArr[i].src != '')
	    		{
	    			return '<tr><td><img src="'+imagesArr[i].src+'"/></td></tr>';
	    		}
	    	}
    	}
	},
	InitCrawler : function(){
		 document.body.insertAdjacentHTML('afterBegin', imageCrawler.GenerateContainer());
	}
};

 (function(){
 	setTimeout("imageCrawler.InitCrawler()", 50);
 })();